<?php


namespace App\B2BProject\Site;

use App\B2BProject\AttachmentPrivateFiles;
use App\B2BProject\Custom\Business\Goods;
use App\B2BProject\Custom\Business\GoodsCategory;
use App\B2BProject\Custom\HotWord;
use App\B2BProject\Custom\SiteSetting;

trait HelperControllerBuildTrait
{
    protected function buildModuleVariable()
    {
        // 底部数据
        $categories = ArticleCategory::getCategoryCollectionByCategoryName('helper');

        foreach ($categories->take(6)->get() as $category) {
            $data[] = [
                'item_desc' => $category->display_name,
                'item_list' => call_user_func_array(function ($articles, $category) {
                    $data = [];
                    foreach ($articles as $article) {
                        $data[] = [
                            'href_val' => route('industry.comm', ["$category->name", $article->id]),
                            'href_txt' => $article->title
                        ];
                    }
                    return $data;
                }, [$category->articles()->take(5)->get(), $category])
            ];
        }
        view()->share('helpData', json_encode($data));
        //底部信息
        $bottomData=[];
        foreach (ArticleCategory::getArticleCollectionByCategoryName('dbxx')->get() as $article) {
            $bottomData[]= [
                'href_src' => route('industry.comm', ["dbxx", $article->id]),
                'href_text' =>  $article->title
            ];
        }

        view()->share('bottomData', json_encode($bottomData));

        //热门关键字
        $heartData                 = [];

        $heartData['web_logo_img'] = AttachmentPrivateFiles::where('id',
            SiteSetting::where('id',1)->first()->logo)->first()->adminRoute();
        $heartData['home_url']     = route('host');
        $heartData['d_code_img']   = "/assets/b2b/css/images/d-code.jpg";
        $heartData['csrfToken']   = csrf_token();

        foreach (HotWord::orderBy('created_at',
            'desc')->take(5)->get() as $category) {
            $heartData['hot_words'][] = [
                'href_val' => route('free.choose',['cid'=>$category->id,'pid'=>$category->parent_id]),
                'word_txt' => $category->name
            ];
        }
        view()->share('heartData', json_encode($heartData));
    }

}